// pages/serve/application/application.js

var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    array: ['男', '女'],
    index: 0,
    multiIndex: [0, 0, 0],
    date: '2019-01-01',
    time: '12:01',
    region: ['请选择', '请选择', '请选择'],
    customItem: '全部',
    addressDetails:"选择地址"
  },

  // bindPickerChange: function (e) {
  //   // console.log('picker发送选择改变，携带值为', e.detail.value)bindRegionChange
  //   this.setData({
  //     index: e.detail.value
  //   })
  // },
  // bindMultiPickerChange: function (e) {
  //   // console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     multiIndex: e.detail.value
  //   })
  // },
  
  // bindDateChange: function (e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     date: e.detail.value
  //   })
  // },
  // bindTimeChange: function (e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     time: e.detail.value
  //   })
  // },

  //地址选择
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },



  formSubmit: function (e) {
    var that = this
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let list = e.detail.value
    console.log(list.name)
    if(list.name == ''){
      wx.showToast({
        title: "请输入姓名",
        icon: 'none',
        duration: 1000
      })
      return false
    }
    if (list.age == '') {
      wx.showToast({
        title: "请输入年龄",
        icon: 'none',
        duration: 1000
      })
      return false
    }
    if (list.phone == '') {
      wx.showToast({
        title: "请输入联系电话",
        icon: 'none',
        duration: 1000
      })
      
      return false
    }else{
      if (list.phone.length < 11 || !myreg.test(list.phone)) {
        wx.showToast({
          title: "联系电话输入有误",
          icon: 'none',
          duration: 1000
        })
        return false
      }
    }
    let address = list.address
    let addressJson = ''
    for (let i = 0; i < address.length;i++){
      if (address[i] == '请选择') {
        wx.showToast({
          title: "请选择联系地址",
          icon: 'none',
          duration: 1000
        })
        return false
      }else{
        addressJson = addressJson + address[i]
      }
    }
    if (that.data.addressDetails == '选择地址'){
      wx.showToast({
        title: "请选择地址详情",
        icon: 'none',
        duration: 1000
      })
      return false
    }
    addressJson = addressJson + that.data.addressDetails
    let newList = {}
    newList.name = list.name
    newList.age = list.age
    newList.sex = list.sex
    newList.phone = list.phone
    newList.address = addressJson
    newList.other = list.other
    console.log(newList)
    let val = app.searchWord
    lists(JSON.stringify(newList), val.substring(val.indexOf("_") + 1))

    // console.log(addressJson)
    // if (list.address == '') {
    //   wx.showToast({
    //     title: "请输入姓名",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false
    // }
    // if (list.name == '') {
    //   wx.showToast({
    //     title: "请输入姓名",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false
    // }if(list.name == ''){
    //   wx.showToast({
    //     title: "请输入姓名",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false
    // }

    // var val = JSON.stringify(e.detail.value)
    // console.log(val)
  },
  // formReset: function () {
  //   console.log('form发生了reset事件')
  // },
  //获取位置
  address(e){
    var that = this
    let region = that.data.region
    wx.chooseLocation({
      success:function(res){
        console.log(res)
        that.setData({
          addressDetails:res.name
          
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
//表单提交
function lists(json,id) {
    wx.request({
      url: 'https://minitest.17jxyx.com/api/v1/Userform/form',
      method: "POST",
      header: {
        token: app.token
      },
      data: {
        content: json,
        wu_id:id
      },
      success: function (res) {
        console.log(res)
        // if (res.error_code == 60000)
      },
      fail: function (res) {
        console.log(res)
      }
    })
}